|
|
|
|
|
|
|
Now that you know how to use the SELECT_FROM statement to select the desired rows and columns from data tables, read about how to use SQL functions to calculate and manipulate data within your selected columns and rows. |
|
|
|
|
|
|
|
|
The SQL standards define a core set of functions that are present in all SQL-compliant systems. These functions are known as aggregate functions. Aggregate functions are used to quickly return computed results of numeric data stored in a column. The SQL aggregate functions available through the Microsoft Access Jet database engine are |
|
|
|
|
|
|
|
|
·AVG: Returns the average value of all the values in a column. |
|
|
|
|
|
|
|
|
·COUNT: Returns the number of columns and is usually used to determine the total rows in a view. COUNT is the only standard SQL aggregate function that can be applied to a non-numeric column. |
|
|
|
|
|
|
|
|
·SUM: Returns the total of all the values in a column. |
|
|
|
|
|
|
|
|
·MAX: Returns the highest of all the values in a column. |
|
|
|
|
|
|
|
|
·MIN: Returns the lowest of all the values in a column. |
|
|
|
|
|
|
|
|
The following SQL statement illustrates all five of the SQL aggregate functions. Enter and execute this statement, and check your results against Figure 8.15. |
|
|
|
|
|
|
|
|
SELECT COUNT(Units) AS UnitCount,
AVG(Units) AS UnitAvg,
SUM(Units) AS UnitSum,
MIN(Units) AS UnitMin,
MAX(Units) AS UnitMax
FROM BookSales |
|
|
|
|
|
|
|
|
Figure 8.15.
Using SQL aggregate functions. |
|
|
|
|
|
|
|
|
You can use the WHERE clause and aggregate functions in the same SELECT_FROM statement. The following statement shows how you can use the WHERE clause to limit rows included in the aggregate calculation. See Figure 8.16 for results. Compare these numbers to those in the view returned in the previous query (refer to Figure 8.15). |
|
|
|
|
|